package com.chris.cc.user.controller;

import com.chris.cc.common.exception.InvalidArgumentException;
import com.chris.cc.common.web.R;
import com.chris.cc.user.model.dto.UserDto;
import com.chris.cc.user.service.wrapper.UserServiceWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: haoka (haokang207@126.com)
 * Date: 2018/8/15 14:09
 * 用户
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserServiceWrapper userServiceWrapper;

    /**
     * 当前登录用户信息
     *
     * @param authentication
     * @return
     */
    @GetMapping("/info")
    public R info(Authentication authentication) {
        return R.builder().data(userServiceWrapper.getByTel(authentication.getPrincipal().toString())).build();
    }

    /**
     * 根据手机号获取用户信息
     *
     * @param tel
     * @return
     */
    @GetMapping("/get/{tel}")
    public R getByTel(@PathVariable("tel") String tel) {
        return R.builder().data(userServiceWrapper.getByTel(tel)).build();
    }

    @GetMapping
    public R query(int page, int limit, boolean isAsc, String keyword) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit, Sort.by(isAsc ? Sort.Direction.ASC : Sort.Direction.DESC,
                "name"));
        return R.builder().data(userServiceWrapper.query(null, null, keyword, pageRequest)).build();
    }

    @PostMapping
    public R add(@RequestBody UserDto user) {
        try {
            userServiceWrapper.add(user);
            return R.builder().build();
        } catch (InvalidArgumentException e) {
            log.error("add user failed.", e);
            return R.builder().ret(-1).build();
        }
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.builder().data(userServiceWrapper.getById(id)).build();
    }

    @GetMapping("/{id}/{enable}")
    public R toggle(@PathVariable("id") Long id, @PathVariable("enable") boolean enable) {
        userServiceWrapper.toggle(id, enable);
        return R.builder().build();
    }

    @PutMapping
    public R update(@RequestBody UserDto user) {
        try {
            userServiceWrapper.update(user);
            return R.builder().build();
        } catch (InvalidArgumentException e) {
            log.error("update user failed.", e);
            return R.builder().ret(-1).build();
        }
    }
}
